package com.ljw.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ljw.dto.BookDto;
import com.ljw.dto.CheckOutDto;
import com.ljw.dto.MoveInDto;
import com.ljw.entity.Book;
import com.ljw.entity.Move;
import com.ljw.entity.Movesum;
import com.ljw.mapper.MoveMapper;
import com.ljw.service.BookService;
import com.ljw.service.MoveService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ljw.service.MovesumService;
import com.ljw.vo.MoveVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * <p>
 * 入住信息表 服务实现类
 * </p>
 *
 * @author ljw
 * @since 2020-07-25
 */
@Service
public class MoveServiceImpl extends ServiceImpl<MoveMapper, Move> implements MoveService {

    @Autowired
    private MoveMapper moveMapper;

    @Autowired
    private BookService bookService;

    @Autowired
    private MovesumService movesumService;

    @Override
    public Page<MoveVo> findAll(Page<MoveVo> page, int userId) {
        return moveMapper.findAll(page,userId);
    }

    @Override
    public String moveIn(MoveInDto moveInDto) {
        //search in book
        QueryWrapper<Book> bookQueryWrapper = new QueryWrapper<>();
        bookQueryWrapper.eq("book_id",moveInDto.getBookId());
        Book book = bookService.getOne(bookQueryWrapper);
        if(book == null) return "ERROR";

        //insert into move
        Date date = new Date();
        Move move = new Move();
        move.setMoveOrderNum(book.getBookOrderNum());
        move.setMoveUserId(book.getBookUserId());
        move.setMoveRoomId(book.getBookRoomId());
        move.setMoveInDate(book.getBookInDate());
        move.setMoveSumPrice(book.getBookSumPrice());
        move.setMoveOutDate(book.getBookOutDate());
        save(move);

        //insert into movesum
        Movesum movesum = new Movesum();
        movesum.setMovesumOrderNum(book.getBookOrderNum());
        movesum.setMovesumUserId(book.getBookUserId());
        movesum.setMovesumRoomId(book.getBookRoomId());
        movesum.setMovesumInDate(book.getBookInDate());
        movesum.setMovesumSumPrice(book.getBookSumPrice());
        movesum.setMovesumOutDate(book.getBookOutDate());
        movesum.setMovesumType("入住");
        movesum.setMovesumCreateTime(date);
        movesum.setMovesumCreateBy(moveInDto.getCreateBy());
        movesumService.save(movesum);

        //remove from book
        bookService.removeById(moveInDto.getBookId());
        return "OK";
    }

    @Override
    @Transactional
    public String checkOut(CheckOutDto checkOutDto){
        //search in move
        QueryWrapper<Move> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("move_id",checkOutDto.getMoveId());
        Move move = getOne(queryWrapper);

        //insert into movesum
        Date date = new Date();
        Movesum movesum = new Movesum();
        movesum.setMovesumOrderNum(move.getMoveOrderNum());
        movesum.setMovesumUserId(move.getMoveUserId());
        movesum.setMovesumRoomId(move.getMoveRoomId());
        movesum.setMovesumInDate(move.getMoveInDate());
        movesum.setMovesumSumPrice(move.getMoveSumPrice());
        movesum.setMovesumOutDate(move.getMoveOutDate());
        movesum.setMovesumType("退房");
        movesum.setMovesumCreateTime(date);
        movesum.setMovesumCreateBy(checkOutDto.getCreateBy());
        movesumService.save(movesum);

        //update move
        removeById(checkOutDto.getMoveId());

        return "OK";
    }

    @Override
    @Transactional
    public String bookAndMoveIn(BookDto bookDto) {
        System.out.println(bookDto);
        int bookId = bookService.book(bookDto);
        if(bookId == 0) return "ERROR";
        MoveInDto moveInDto = new MoveInDto();
        moveInDto.setBookId(bookId);
        moveInDto.setCreateBy(bookDto.getCreateBy());
        System.out.println(moveInDto);
        String res2 = moveIn(moveInDto);
        return bookId > 0 &&res2.equalsIgnoreCase("OK")?"OK":"ERROR";
    }

}
